package top.v5it.japi.plus.common.func;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * 自定义函数接口
 *
 * @author zhanpu
 * @date 2018/12/18
 */
@FunctionalInterface
public interface Func0<T, E extends Throwable> {

    /**
     * Gets a result.
     *
     * @return a result
     * @throws E
     */
    T get() throws E;

    /**
     * 处理往外抛异常
     *
     * @param before
     * @return
     */
    default Supplier<T> wrapper(Supplier<? extends T> before) {
        Objects.requireNonNull(before);

        return () -> {
            try {
                return before.get();
            } catch (Throwable e) {
                throw new FuncException(e);
            }
        };
    }

    /**
     * 基于本函数返回{@link FuncException}
     *
     * @return T 返回值
     */
    default T getWithFuncException() {
        try {
            return get();
        } catch (Throwable e) {
            throw new FuncException(e);
        }
    }
}
